package com.fanfan.runner;

import com.fanfan.dao.ArticleDao;
import com.fanfan.pojo.Article;
import com.fanfan.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ViewCountRunner implements CommandLineRunner {
    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private RedisCache redisCache;
    @Override
    public void run(String... args) throws Exception {

        List<Article> articles = articleDao.selectList(null);
        Map<String, Integer> viewCountMap = articles.stream().collect(Collectors.toMap(article -> article.getId().toString(), article -> {
            return article.getViewCount().intValue();
        }));
        redisCache.setCacheMap("article:viewCount",viewCountMap);
        System.out.println("Redis加载成功，viewCount已存入redis");
    }
}
